<?php
/**
 * Created by PhpStorm.
 * User: user
 * Date: 2020-05-23
 * Time: 11:22
 */

namespace App\Controllers;


use App\Services\MapServices;
use Illuminate\Support\Facades\Validator;

class MapController extends Controller
{
    /**
     * 行政区域列表
     *
     * @return string
     */
    public function districtList()
    {
        $mapObj = new MapServices();
        $res    = $mapObj->districtList();

        return $res;
    }

    /**
     * 通过关键字搜索
     *
     * @return \Illuminate\Http\JsonResponse|string
     */
    public function searchByKeyWords()
    {
        $params = $this->request->all();

        $validator = Validator::make($params,
            [
                'keyword'  => 'required',
                'boundary' => 'required',
            ]
        );
        if ($validator->fails()) return response()->json(['code' => 20000, 'msg' => $validator->errors()->first(), 'data' => []]);


        $mapObj = new MapServices();
        $res    = $mapObj->search($params['keyword'], $params['boundary']);

        return $res;
    }

    /**
     * 获取指定行政区划的子级行政区划
     *
     * @return \Illuminate\Http\JsonResponse|string
     */
    public function getDistrictById()
    {
        $params = $this->request->all();

        $validator = Validator::make($params,
            [
                'district_id' => 'int',
            ]
        );
        if ($validator->fails()) return response()->json(['code' => 20000, 'msg' => $validator->errors()->first(), 'data' => []]);

        $mapObj = new MapServices();
        $res    = $mapObj->getChildren($params['district_id']);

        return $res;
    }

}